/*
 * ScheduleCreationWindow.java
 * 
 * Copyright (c) 2008, Marshall Scorcio, Ralph Holzmann, 
 *                     Stewart Sweet, Tyler Hoersch, Jeffery Van Handel
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the University of Wisconsin Oshkosh nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;

public class ScheduleCreationWindow extends JFrame implements ItemListener, ListSelectionListener, ActionListener
{
    private EventListenerList listenerList;
    
    private JPanel scheduleCreationPanel;
    private JPanel dayPanel;
    private JPanel shiftPanel;
    
    private JLabel dateRangeLabel;
    private JLabel dayLabel;
    private JLabel shiftLabel;
    private JPanel employeePanel;
    private JLabel employeeLabel;
    
    private JList dayList;
    private JList shiftList;
    private JList employeeList;
    
    private DefaultListModel dayListModel;
    private DefaultListModel shiftListModel;
    private DefaultListModel employeeListModel;
    
    private JScrollPane dayScrollPane;   
    private JScrollPane shiftScrollPane;
    private JScrollPane employeeScrollPane;
    
    private JButton addShiftButton;
    private JButton removeShiftButton;
    private JButton addEmployeeToShift;
    private JButton delEmployeeToShift;
    private JButton viewScheduleButton;
    private JButton cancelButton;
    private JButton applyButton;
    
    private Box addDelEmployeeBox;
    private Box addRemoveShiftBox;
    private Box cancelApplyBox;
    
    private JCheckBox showAll;
   
    public ScheduleCreationWindow()
    {
        super("Create Schedule");
        setSize(1150, 745);   
        
        initComponents();
        rootPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        listenerList = new EventListenerList();
    }
    
    private void initComponents()
    {
        dateRangeLabel = new JLabel("Week Of: ");
        
        dayPanel = new JPanel(new BorderLayout());
        dayLabel = new JLabel("Days");
        dayListModel = new DefaultListModel();
        dayList = new JList(dayListModel);
        dayScrollPane = new JScrollPane(dayList);
        
        shiftPanel = new JPanel(new BorderLayout());
        shiftLabel = new JLabel("Shifts");
        shiftListModel = new DefaultListModel();
        shiftList = new JList(shiftListModel);
        shiftScrollPane = new JScrollPane(shiftList);
        addShiftButton = new JButton("Add Shift");
        removeShiftButton = new JButton("Remove Shift");
        addRemoveShiftBox = Box.createHorizontalBox();
        
        employeePanel = new JPanel(new BorderLayout());
        employeeLabel = new JLabel("Employees");
        employeeListModel = new DefaultListModel();
        employeeList = new JList();
        employeeScrollPane = new JScrollPane(employeeList);
        
        showAll = new JCheckBox("All Employees");
        
        scheduleCreationPanel = new JPanel(new GridLayout(1, 3));

        viewScheduleButton = new JButton("View Current Schedule");
        cancelButton = new JButton("Cancel");
        applyButton = new JButton("Apply");
        cancelApplyBox = Box.createHorizontalBox();
        
        addEmployeeToShift = new JButton("Add Employee");
        delEmployeeToShift = new JButton("Delete Employee");
        addDelEmployeeBox = Box.createHorizontalBox();

        dateRangeLabel.setHorizontalAlignment(JLabel.CENTER);
    
        dayList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        dayList.addListSelectionListener(this);
        dayPanel.add(dayLabel, BorderLayout.NORTH);
        dayPanel.add(dayScrollPane, BorderLayout.CENTER);
        dayPanel.add(cancelApplyBox, BorderLayout.SOUTH);

        shiftList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
        shiftList.addListSelectionListener(this);
        
        addRemoveShiftBox.add(Box.createHorizontalGlue());
        addRemoveShiftBox.add(addShiftButton);
        addRemoveShiftBox.add(removeShiftButton);     
        
        showAll.addActionListener(this);
        addEmployeeToShift.addActionListener(this);
        delEmployeeToShift.addActionListener(this);
        
        addDelEmployeeBox.add(Box.createHorizontalGlue());
        addDelEmployeeBox.add(showAll);
        addDelEmployeeBox.add(addEmployeeToShift);
        addDelEmployeeBox.add(delEmployeeToShift);
        
        dayPanel.add(dayLabel, BorderLayout.NORTH);
        dayPanel.add(dayScrollPane, BorderLayout.CENTER);
        dayPanel.add(cancelApplyBox, BorderLayout.SOUTH);
        
        shiftPanel.add(shiftLabel, BorderLayout.NORTH);
        shiftPanel.add(shiftScrollPane, BorderLayout.CENTER);
        shiftPanel.add(addRemoveShiftBox, BorderLayout.SOUTH);

        employeeList.addListSelectionListener(this);
        employeePanel.add(employeeLabel, BorderLayout.NORTH);
        employeePanel.add(employeeScrollPane, BorderLayout.CENTER);
        employeePanel.add(addDelEmployeeBox, BorderLayout.SOUTH);
    
        scheduleCreationPanel.add(dayPanel);
        scheduleCreationPanel.add(shiftPanel);
        scheduleCreationPanel.add(employeePanel);      
        
        viewScheduleButton.addActionListener(this);
        applyButton.addActionListener(this);
        cancelButton.addActionListener(this);
        addShiftButton.addActionListener(this);
        removeShiftButton.addActionListener(this);
        addEmployeeToShift.addActionListener(this);
        delEmployeeToShift.addActionListener(this);
        
        cancelApplyBox.add(Box.createHorizontalGlue());
        cancelApplyBox.add(viewScheduleButton);
        cancelApplyBox.add(cancelButton);
        cancelApplyBox.add(applyButton); 

        setLayout(new BorderLayout());
        add(dateRangeLabel, BorderLayout.NORTH);
        add(scheduleCreationPanel, BorderLayout.CENTER);
        
        addShiftButton.setEnabled(false);
        removeShiftButton.setEnabled(false);
        showAll.setEnabled(false);
        addEmployeeToShift.setEnabled(false);
        delEmployeeToShift.setEnabled(false);
    }
    
    public boolean isShowAllEmployeesChecked()
    {
        return showAll.isSelected();
    }
        
    public void setDays(String[] days)
    {
        dayListModel.removeAllElements();
        
        for (int i = 0; i < days.length; i++)
        {
            dayListModel.addElement(days[i]);
        }   
        
        dayList.setModel(dayListModel);
    }
    
    public void setShifts(String[] shifts)
    {
        shiftListModel.removeAllElements();
        
        for (int i = 0; i < shifts.length; i++)
        {
            shiftListModel.addElement(shifts[i]);
        }
        
        shiftList.setModel(shiftListModel);
    }
    
    public void addShift(String shift)
    {
        shiftListModel.addElement(shift);
        shiftList.setModel(shiftListModel);
    }
       
    public void clearShifts()
    {
        shiftListModel.removeAllElements();
        shiftList.setModel(shiftListModel);
    }
       
    public void setEmployees(String[] employees)
    {
        employeeListModel.removeAllElements();
        
        for (int i = 0; i < employees.length; i++)
        {
            employeeListModel.addElement(employees[i]);
        }
        
        employeeList.setModel(employeeListModel);
    }
    
    public void clearEmployees()
    {
        employeeListModel.removeAllElements();
        employeeList.setModel(employeeListModel);
    }
     
    public void setDateRange(String dateRange)
    {
        dateRangeLabel.setText(dateRange);
    }
    
    public String[] getSelectedEmployees()
    {      
        Object[] selectedEmployees = employeeList.getSelectedValues();
        
        String[] employees = new String[selectedEmployees.length];
        
        for (int i = 0; i < employees.length; i++)
        {
            employees[i] = (String) selectedEmployees[i];
        }

        return employees;
    }
    
    public String getSelectedShift()
    {
        return (String) shiftList.getSelectedValue();
    }
    
    public String getSelectedDay()
    {
        return (String) dayList.getSelectedValue();
    }
    
    public void addShiftButtonPressed()
    {
        ShiftDialog shiftDialog = new ShiftDialog(this);
        shiftDialog.setLocationRelativeTo(this);
        shiftDialog.setVisible(true);
        
        if (shiftDialog.getCloseCode() == ShiftDialog.CONFIRM_CLOSE)
        {
            fireViewEvent(shiftDialog, "AddShift");
        }
    }
    
    public void addViewEventListener(ViewEventListener listener)
    {
        listenerList.add(ViewEventListener.class, listener);
    }
    
    public void removeViewEventListener(ViewEventListener listener)
    {
        listenerList.remove(ViewEventListener.class, listener);
    }
    
    private void fireViewEvent(Object source, String name)
    {
        ViewEvent event = new ViewEvent(source, name);
    
        ViewEventListener[] listeners = listenerList.getListeners(ViewEventListener.class);

        for (int i = 0; i < listeners.length; i++)
        {
            listeners[i].viewEventOccurred(event);
        }
    }

    public void itemStateChanged(ItemEvent e)
    {
        if (e.getSource().equals(showAll))
        {
            fireViewEvent(showAll,"ShowAllEmployeesToggled");
        }
    }

    public void actionPerformed(ActionEvent event)
    {
        if (event.getSource() == applyButton)
        {
            fireViewEvent(applyButton, "Apply");
        }
        else if (event.getSource() == viewScheduleButton)
        {
            fireViewEvent(viewScheduleButton, "ViewSchedule");
        }
        else if (event.getSource() == cancelButton)
        {
            fireViewEvent(cancelButton, "Cancel");
        }
        else if (event.getSource() == addShiftButton)
        {
            addShiftButtonPressed();
        }
        else if (event.getSource() == removeShiftButton)
        {
            fireViewEvent(removeShiftButton, "RemoveShift");
        }
        else if (event.getSource() == showAll)
        {
            fireViewEvent(showAll, "ShowAllEmployeesToggled");
        }
        else if (event.getSource() == addEmployeeToShift)
        {
            fireViewEvent(addEmployeeToShift, "AddEmployeeToShift");
        }
        else if (event.getSource() == delEmployeeToShift)
        {  
            fireViewEvent(delEmployeeToShift, "DeleteEmployeeFromShift");
        }
    }
    
    public void valueChanged(ListSelectionEvent event)
    {
        if (event.getSource() == dayList)
        {
            if (!dayList.isSelectionEmpty())
            {
                addShiftButton.setEnabled(true);
                fireViewEvent(dayList, "DaySelectionChanged");
            }
            else
            {
                addShiftButton.setEnabled(false);
            }
        }
        else if (event.getSource() == shiftList)
        {
            if (!shiftList.isSelectionEmpty())
            {
                removeShiftButton.setEnabled(true);
                addEmployeeToShift.setEnabled(true);
                showAll.setEnabled(true);
                fireViewEvent(shiftList, "ShiftSelectionChanged");
            }
            else
            {
                removeShiftButton.setEnabled(false);
                addEmployeeToShift.setEnabled(true);
                showAll.setEnabled(true);
            }
        }
        else if (event.getSource() == employeeList)
        {
            if (!employeeList.isSelectionEmpty())
            {
                delEmployeeToShift.setEnabled(true);
                fireViewEvent(employeeList, "EmployeeSelectionChanged");
            }
            else
            {
                delEmployeeToShift.setEnabled(false);
            }
        }
    }
}

